cap frame change default
cap frame drop results
cap frame drop disclosure
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evict_all b_judgmentamount se_judgmentamount

global cluster_ols="judgeXyear "
global cluster_iv="judgeXyear "
global instrument="stringency_n100_loo"
global fe="districtXquarter"

macro define controls="noprior joint ad_damnum ageatcase age_sq age_cub fem1 fem1_miss neigh_pov_rate_5yr nbrhd_med_rent_5yr neigh_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss"
local outcomes="fin_health VANTAGE_V3_SCORE zerorevolving ALL5042 ANY_AUT_OR_LEASE inquiries any_inquiries accounts any_accounts "

use unique_id evicted  $cluster_ols  $cluster_iv $fe $instrument $controls ageatcase age_sq age_cub ageatcase_miss *full_samp* VANTAGE_V3_SCORE* ALL5042_* zerorevolving* COL5060* ANY_AUT_OR_LEASE* fin_health_* inquiries* any_inquiries* accounts* any_accounts* stringency_amount judgment_amount exp_indiv_id using "${build}/iv_fin_strain.dta", clear

keep if ageatcase>=18 & ageatcase<=55
rename fem1 fem

*Windsorize judgment amount at $100K to align with Census analysis
replace judgment_amount=100000 if judgment_amount>100000 & judgment_amount<.
replace judgment_amount=0 if judgment_amount==.

* Since payday loan outcomes are a flow measure rather than a stock, we set to missing the outcomes for people who are not observed in the full outcome window
foreach rng in "1_4" "5_8" "9_16" "17_24" {
		foreach name in "inquiries" "any_inquiries" "accounts" "any_accounts" {
			
			if "`name'"=="inquiries" | "`name'"=="any_inquiries" local samp="inq"
			if "`name'"=="accounts"  | "`name'"=="any_accounts"  local samp="trade"
			
			replace `name'_`rng'=. if full_samp_`samp'_`rng'~=1
	}
}

***********************
* MAIN IV REGRESSIONS *
***********************
gen all=1

foreach pop in "all" "fem" {
	foreach popval of numlist 0 1  {
	
			if "`pop'"~="all" | `popval'~=0 {

			qui foreach var in `outcomes' {
				foreach rng in "1_4" "5_8" "9_16" "17_24" {

				*Label variables
				if "`var'"=="VANTAGE_V3_SCORE" label var `var'_`rng' "Credit Score"
				if "`var'"=="zerorevolving" label var `var'_`rng' "No Open Revolving Account"
				if "`var'"=="ALL5042" label var `var'_`rng' "Total bal. delinquencies or collections"
				if "`var'"=="ANY_AUT_OR_LEASE" label var `var'_`rng' "Any Auto Loan or Lease"
				if "`var'"=="any_inquiries" label var `var'_`rng' "Any Payday Inquiry (*100)"
				if "`var'"=="inquiries" label var `var'_`rng' "Number of Payday Inquiries"
				if "`var'"=="any_accounts" label var `var'_`rng' "Any Payday Account (*100)"	
				if "`var'"=="accounts" label var `var'_`rng' "Number of Payday Accounts"
				if "`var'"=="fin_health" label var `var'_`rng' "Financial health index"
				local vname=`"`:variable label `var'_`rng''"'

				local lags="`var'_b1_8 `var'_b1_8_miss `var'_b1 `var'_b2 `var'_b3 `var'_b4 `var'_b1_miss `var'_b2_miss `var'_b3_miss `var'_b4_miss "
				
				if "`rng'"=="b1_8" {
					local lags="`var'_b9_16 `var'_b9_16_miss `var'_b9 `var'_b10 `var'_b11 `var'_b12 `var'_b9_miss `var'_b10_miss `var'_b11_miss `var'_b12_miss "
					recode `var'_`rng' (-1=.)
				}
				
				**IV BASELINE: All specifications are restricted to the same obs as the main IV specification*
				if "`var'"=="inquiries" |  "`var'"=="any_inquiries" ivregress 2sls `var'_`rng' i.$fe $controls (evicted=$instrument) if `pop'==`popval' & full_samp_inq_`rng'==1, r cluster($cluster_iv) 
				if "`var'"=="accounts"  |  "`var'"=="any_accounts" ivregress 2sls `var'_`rng' i.$fe $controls (evicted=$instrument) if `pop'==`popval' & full_samp_trade_`rng'==1, r cluster($cluster_iv) 
				cap drop samplerestriction
				gen samplerestriction=e(sample)

				*NON-EVICTED MEAN
				sum `var'_`rng' if samplerestriction==1 & evicted==0 & `pop'==`popval'
					local y_base_mean=r(mean)
					local y_base_sd=r(sd)
				unique exp_indiv_id if samplerestriction==1 & evicted==0 & `pop'==`popval'
					local y_base_n=r(sum)
				sum `var'_`rng' if samplerestriction==1 & `pop'==`popval'
					if "`pop'"=="all" local sd_y_all=r(sd)
					if "`pop'"~="all" local sd_y_all=.
				sum evicted if samplerestriction==1 & `pop'==`popval'
					if "`pop'"=="all" local sd_evict_all=r(sd)
					if "`pop'"~="all" local sd_evict_all=.
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng'"	
				frame post results ("`label'")(.) (.) (.) (`y_base_mean') (`y_base_sd') (`y_base_n') (`sd_y_all')(`sd_evict_all')(.)(.)
						
				**OLS W/ ADDITIONAL CONTROLS
				reg  `var'_`rng' evicted $controls `lags' i.$fe  if `pop'==`popval' & samplerestriction==1, r cluster($cluster_ols)
				local b_evict=_b[evicted] 
				local se_evict=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng', Spec: OLS baseline w/ additional controls" 
				frame post results ("`label'") (`b_evict') (`se_evict') (`n_obs') (.) (.) (.) (.) (.) (.)(.)
					
				**RF W/ ADDITIONAL CONTROLS
				reg `var'_`rng' $instrument $controls `lags' i.$fe if `pop'==`popval' & samplerestriction==1, r cluster($cluster_iv) 
				local b_evict=_b[$instrument] 
				local se_evict=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng', Spec: RF: stringency w/ additional controls" 
				frame post results ("`label'") (`b_evict') (`se_evict') (`n_obs') (.) (.) (.) (.) (.) (.)(.)		

				**IV W/ ADDITIONAL CONTROLS	
				ivregress 2sls `var'_`rng' $controls `lags' i.$fe  (evicted=$instrument) if `pop'==`popval' & samplerestriction==1, r cluster($cluster_iv) 
				local b_evict=_b[evicted] 
				local se_evict=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng', Spec: IV baseline w/ additional controls" 
				frame post results ("`label'") (`b_evict') (`se_evict') (`n_obs') (.) (.) (.) (.) (.) (.)(.)	
							
				if "`pop'"=="all" {
					**IV W/ 1 instrument and control for amount stringency 	
					ivregress 2sls `var'_`rng' (evicted=$instrument) stringency_amount  $controls `lags' i.$fe   if `pop'==`popval' & samplerestriction==1 & joint==1 & judgment_amount~=., r cluster($cluster_iv) 
					local b_evict=_b[evicted] 
					local se_evict=_se[evicted] 
					local n_obs=e(N)
					local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng', Spec: IV baseline w/ 1 instrument and control for amount stringency" 
					frame post results ("`label'") (`b_evict') (`se_evict') (`n_obs') (.) (.) (.) (.) (.) (.)(.)		
					}
				}
			}
		}
	}
}

frame results: export delimited using "${Inputs}\Cook_experian_ivols.csv", replace
